﻿using System.Linq;
using System.Net;
using System.Web;
using BrettM.Web.Restful;

namespace BrettM.Web.Http
{
    public static class ETagHelper
    {

        public const string Wildcard = "*";

        public static HttpStatusCode IfMatch(HttpRequestBase request, object model)
        {
            var eTagModel = model as IETagable;
            if (eTagModel == null)
                return HttpStatusCode.OK;

            var modelTag = eTagModel.ETag;

            var matchAll = request.Headers["If-Match"];
            if (string.IsNullOrEmpty(matchAll))
                return HttpStatusCode.OK;

            var matchList = ETag.ParseETags(matchAll);

            return matchList.Any(s => s == modelTag) ? HttpStatusCode.OK : HttpStatusCode.NotFound;
        }


        public static HttpStatusCode IfNotMatch(HttpRequestBase request, object model)
        {
            var result = IfMatch(request, model);

            if (result != HttpStatusCode.OK)
            {
                var ifModifiedSince = request.Headers["If-Modified-Since"];
                if(!string.IsNullOrEmpty(ifModifiedSince ))
                {
                    
                }
            }
            return result;

        }

    }
}
